#include "calllater.h"

CallLater::CallLater(QObject *parent)
    :QObject{parent}
{
    connect(this, &CallLater::callLater, this, &CallLater::onCallLater, Qt::QueuedConnection);
}

CallLater &CallLater::getInstance()
{
    static CallLater callLater;
    return callLater;
}

void CallLater::onCallLater(CallInfo info)
{
    if(info.isMethod) {
        Q_ASSERT(info.obj != nullptr && info.method != nullptr);
//        qDebug() << "later method:" << info.obj << info.method;
        QMetaObject::invokeMethod(info.obj, info.method, Qt::QueuedConnection, Q_ARG(QVariant, info.arg));
    } else {
//        qDebug() << "later callabck:" << &info.callback << info.arg;
        info.callback(info.arg);
    }
}
